import Mock from 'mockjs';
import detailData from "./detailInfo.json";
//state中0代表未付款  1待评价 2待发货中  3待确认收货中 4退款中 5是所有数据
let allOrderLists = [
    {
        id: 11, //生成ID
        orderInfo: "零基础古典身韵元素组合", //订单信息
        quantity: "2",//购买数量
        price: "48.00",//单价
        payment: "96.00",//实付金额
        state: "0",//状态
        stateInfo: "待付款",//状态信息
    }, {
        id: 22, //生成ID
        orderInfo: "零基础古典手位元素组合", //订单信息
        quantity: "1",
        price: "58.00",
        payment: "58.00",
        state: "0",
        stateInfo: "待付款",
    }, {
        id: 33, //生成ID
        orderInfo: "郭青天原创编舞《春三月》分解教学", //订单信息
        quantity: "1",
        price: "168.00",
        payment: "168.00",
        state: "0",
        stateInfo: "待付款",
    }, {
        id: 44, //生成ID
        orderInfo: "古典长扇青花瓷", //订单信息
        quantity: "2",
        price: "128.00",
        payment: "256.00",
        state: "1",
        stateInfo: "待评价",
    }, {
        id: 55, //生成ID
        orderInfo: "原创蒙古族舞蹈《梦中的妈妈》", //订单信息
        quantity: "1",
        price: "66",
        payment: "66.00",
        state: "1",
        stateInfo: "待评价",
    }, {
        id: 66, //生成ID
        orderInfo: "地面综合表演性身韵《笑眄》", //订单信息
        quantity: "10",
        price: "38.00",
        payment: "380.00",
        state: "2",
        stateInfo: "发货中",
    }, {
        id: 77, //生成ID
        orderInfo: "古典舞身韵盘腕组合《半壶纱》", //订单信息
        quantity: "2",
        price: "48.00",
        payment: "96.00",
        state: "2",
        stateInfo: "发货中",
    }, {
        id: 88, //生成ID
        orderInfo: "《机械舞教学系列》第二集 肩部多种训练步法", //订单信息
        quantity: "1",
        price: "9.99",
        payment: "9.99",
        state: "3",
        stateInfo: "确认收货中",
    }, {
        id: 99, //生成ID
        orderInfo: "庞思妤舞蹈作品《绝世舞姬》教学", //订单信息
        quantity: "3",
        price: "68.00",
        payment: "204.00",
        state: '3',
        stateInfo: "确认收货中",
    }, {
        id: 101, //生成ID
        orderInfo: "《我爱你中国》演出加长版 教学课", //订单信息
        quantity: "1",
        price: "88.00",
        payment: "88.00",
        state: "3",
        stateInfo: "确认收货中",
    }, {
        id: 112, //生成ID
        orderInfo: "蒙族舞《陪你一起看草原》", //订单信息
        quantity: "5",
        price: "50.00",
        payment: "250.00",
        state: "4",
        stateInfo: "退款中",
    }, {
        id: 123, //生成ID
        orderInfo: "危险派对", //订单信息
        quantity: "4",
        price: "4.00",
        payment: "16.00",
        state: "4",
        stateInfo: "退款中",
    }
]

Mock.mock(RegExp("/mock/order/allOrderList" + ".*"), "get", (options) => {
    //数据持久化
    if (!JSON.parse(localStorage.getItem("ORDER_ALL"))) {
        localStorage.setItem("ORDER_ALL", JSON.stringify(allOrderLists));
    }
    let allOrderList = JSON.parse(localStorage.getItem("ORDER_ALL"));
    let state = "";
    if (options.url.indexOf("?") === -1) {
        state = "0"
    } else {
        //获取传过来的query参数 只能传0-4的状态值
        state = options.url.split("?")[1].split("=")[1];
    }
    //未付款
    if (state === "0") {
        allOrderList = allOrderList.filter(item => item.state === state)
        //待评价
    } else
    if (state === "1") {
        allOrderList = allOrderList.filter(item => item.state === state)
    } else
        //发货中
    if (state === "2") {
        allOrderList = allOrderList.filter(item => item.state === state)
    } else
        //收货中
    if (state === "3") {
        allOrderList = allOrderList.filter(item => item.state === state)
    } else
        //退款中
    if (state === "4") {
        allOrderList = allOrderList.filter(item => item.state === state)
    }else
        //全部订单
    if (state === "5"){
        allOrderList = [...allOrderList]
    }else {
        return {
            code:201,
            data:[],
            message:"error"
        }
    }
    return {
        code: 200,
        data: allOrderList,
        message: 'success'
    }
})

//从所有状态搜索订单信息   模糊查询数据
Mock.mock(RegExp("/mock/order/keyWord" + ".*"),"get",(options)=>{
    if (options.url.indexOf("?") === -1) {
        return {
            code:201,
            data:[],
            message:"error"
        }
    }
    
    let state = options.url.split("?")[1].split("=")[1];
    const result = allOrderLists.filter(item=>{
        if (item.orderInfo.includes(state)){
            return item;
        }
    })
    return {
        code: 200,
        data: result,
        message: 'success'
    }
})

//个人信息的详情数据请求
Mock.mock("/mock/detail/byId",'get',()=>{
    return {
        code: 200,
        data: detailData,
        message: 'success'
    }
})
